package com.fankui.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fankui.commonutils.R;
import com.fankui.servicebase.exceptionhandler.LegendaryException;
import com.fankui.serviceedu.client.VodClient;
import com.fankui.serviceedu.entity.EduVideo;
import com.fankui.serviceedu.mapper.EduVideoMapper;
import com.fankui.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 范奎
 * @since 2023-11-25
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduVideo> videoList = baseMapper.selectList(wrapper);
        List<String> vodIdList = new ArrayList<>();
        for (EduVideo video : videoList) {
            String vodId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(vodId)) {
                vodIdList.add(vodId);
            }
        }
        if (videoList.size() > 0) {
            // 远程调用删除视频集合的方法删除课程下所有视频
            R result = vodClient.removeVodVideoList(vodIdList);
            if (result.getCode() != 20000) {
                throw new LegendaryException(20001, "删除课程失败！");
            }
        }
        baseMapper.delete(wrapper);
    }
}
